package com.gsy.controller.admin;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsy.aop.annotation.TokenValid;
import com.gsy.entity.LeaveEntity;
import com.gsy.entity.UserEntity;
import com.gsy.enumerate.SystemStatusEnum;
import com.gsy.response.BaseResponse;
import com.gsy.service.LeaveService;
import com.gsy.service.UserService;
import com.gsy.vo.GetLeaveVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gsy
 * @since 2022-03-03
 */
@RestController
@RequestMapping(consumes = "application/json", produces = "application/json")
@Api(value = "controller", tags = {"tb_leave 操作接口"})
public class AdmintLeaveController {

    private final static Logger LOGGER = LoggerFactory.getLogger(AdmintLeaveController.class);

    @Resource
    private LeaveService leaveService;

    @Resource
    private UserService userService;

    /**
     * 获取该老师所属班级的全部请假单与数量(分页查询)
     *
     * @return
     */
    @ApiOperation(value = "获取该老师所属班级的全部请假单与数量(分页查询)", notes = "获取该老师所属班级的全部请假单与数量(分页查询)")
    @GetMapping("/admin/getLeave/{uId}/{currPage}/{pageNum}")
    @TokenValid
    public BaseResponse getLeave(@PathVariable Integer uId, @PathVariable Integer pageNum, @PathVariable Integer currPage) {

        UserEntity userEntity = userService.getById(uId);
        String classes = userEntity.getClasses();
        if (StringUtils.isBlank(classes)) {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "未绑定班级！");
        }
        Page<LeaveEntity> page = leaveService.page(new Page<>(currPage, pageNum), Wrappers.lambdaQuery(new LeaveEntity())
                .like(LeaveEntity::getClasses, classes).orderByDesc(LeaveEntity::getLId));
        List<LeaveEntity> entities = page.getRecords();
        List<Integer> userIdList = entities.stream().map(LeaveEntity::getUId).collect(Collectors.toList());
        List<UserEntity> userEntities = userService.listByIds(userIdList);

        ArrayList<GetLeaveVO> getLeaveVOS = new ArrayList<>();
        for (UserEntity entity : userEntities) {
            for (LeaveEntity leaveEntity : entities) {
                GetLeaveVO vo = new GetLeaveVO();
                BeanUtils.copyProperties(leaveEntity, vo);
                if (leaveEntity.getUId().equals(entity.getId())) {
                    vo.setUsername(entity.getName());
                    vo.setName(entity.getName());
                }
                getLeaveVOS.add(vo);
            }
        }

        if (CollectionUtil.isNotEmpty(getLeaveVOS)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(getLeaveVOS);
            success.setTotal(page.getTotal());
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
    }

    /**
     * 获取该学生请假审批与数量
     *
     * @return
     */
    @ApiOperation(value = "获取该学生请假审批与数量", notes = "获取该学生请假审批与数量")
    @GetMapping("/admin/getUserLeave/{uId}/{currPage}/{pageNum}")
    @TokenValid
    public BaseResponse getUserLeave(@PathVariable Integer uId, @PathVariable Integer pageNum, @PathVariable Integer currPage) {

        Page<LeaveEntity> page = leaveService.page(new Page<>(currPage, pageNum), Wrappers.lambdaQuery(new LeaveEntity())
                .like(LeaveEntity::getUId, uId).orderByDesc(LeaveEntity::getLId));
        List<LeaveEntity> entities = page.getRecords();

        if (CollectionUtil.isNotEmpty(entities)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            success.setTotal(page.getTotal());
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
    }

    /**
     * 当前请假单审批(修改审批状态)
     *
     * @return
     */
    @ApiOperation(value = "当前请假单审批(修改审批状态)", notes = "当前请假单审批(修改审批状态)")
    @GetMapping("/admin/upLeaveState/{lId}/{upState}")
    @TokenValid
    public BaseResponse upLeaveState(@PathVariable Integer lId, @PathVariable Integer upState) {

        LeaveEntity leaveEntity = leaveService.getById(lId);
        if (leaveEntity == null) {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "该记录不存在！");
        }
        leaveEntity.setState(upState);
        leaveService.updateById(leaveEntity);
        return new BaseResponse(SystemStatusEnum.SUCCESS);
    }

}
